package io;

/**
 * @author weim
 * @date 18-8-28
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//字节流操作的数据单元是8位的字节
//字符流操作的数据单元是16位的字符
//字节流和字符流都输与节点流                 节点流的的构造器参数是一个物理节点   比如一个文件
//处理流             构造参数是一个 节点流
public class ReadWrite2 {

    public static void main(String[] args) throws IOException {

        readWriteFileInputStream(new File("D:\\MyExecutorService.docx"),new File("D:\\copy.docx"));
        System.out.println("---------------------------------");
        readWriteFileReader(new File("D:\\MyExecutorService.txt"),new File("D:\\copy2.txt"));
        System.out.println("---------------------------------");
    }
    //字节流
    public static void readWriteFileInputStream(File inFile,File outFile) throws IOException{
        //创建字节输入流
        FileInputStream input = new FileInputStream(inFile);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(input);

        //输出流
        FileOutputStream output = new FileOutputStream(outFile);
        BufferedOutputStream outputStream = new BufferedOutputStream(output);

        byte[] buf = new byte[1024];
        while(bufferedInputStream.read(buf) > 0) {
            System.out.println(new String(buf));
            outputStream.write(buf);
        }
        output.flush();
        outputStream.close();
        bufferedInputStream.close();
        //处理流包装了节点流之后，关闭输入输出流时，只要关闭最上层的处理流即可，系统自动关闭被该处理流包装的节点流
        //output.close();
        //input.close();
        //outFile.delete();
    }
    //字符流
    public static void readWriteFileReader(File inFile,File outFile) throws IOException{
        FileReader input = new FileReader(inFile);
        BufferedReader bReader = new BufferedReader(input);
        FileWriter output = new FileWriter(outFile);
        BufferedWriter outwriter = new BufferedWriter(output);

        //char[] buf = new char[512];
        String line = null;
        while((line = bReader.readLine()) != null) {
            System.out.println(line);
            outwriter.write(line);
        }
        outwriter.flush();
        outwriter.close();
        bReader.close();
        //处理流包装了节点流之后，关闭输入输出流时，只要关闭最上层的处理流即可，系统自动关闭被该处理流包装的节点流
        //output.close();
        //input.close();
        //outFile.delete();
    }
}
